package com.common.library.util;

import java.io.File;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

/**
 * 
 * function: 下载图片线程
 *
 * @author  aXiBa
 */
public class DownLoadThread extends Thread {
	
	private static final String TAG = DownLoadThread.class.getSimpleName();
	
	public static final int MSG_KEY_DOWNIMAGE_SUCCESS = -9999;
	
	public static final String PARAM_KEY_DOWNIMG_SERVERPATH = "SERVERPATH";
	
	public static final String PARAM_KEY_DOWNIMG_LOCALPATH = "LOCALPATH";
	
	public static final String PARAM_KEY_DOWNIMG_FILENAME = "FILENAME";
	
	public static final String PARAM_KEY_DOWNIMG_TAG = "DOWNIMG_TAG";
	
	Handler handler;
	
	String url;
	String localPath;
	String fileName;
	String subLocalPath;
	String default_url;
	boolean isUseDefault = false;

	// 多线程时为区分还要传入tag 
	String tag = "";
	
	//是否有网络
	boolean isNetworkAvailable = false;
	
	
	public DownLoadThread(String url, Handler handler, boolean isNetworkAvailable) {
		
		this.url = url;
		
		this.handler = handler;
		this.isNetworkAvailable = isNetworkAvailable;
	}
	
	
	public DownLoadThread(String url, Handler handler, String tag, boolean isNetworkAvailable) {
		
		this.url = url;
		
		this.handler = handler;
		this.tag = tag;
		
		this.isNetworkAvailable = isNetworkAvailable;
	}
	
	
	public DownLoadThread(String url, Handler handler, String tag, String subLocalPath, boolean isNetworkAvailable) {
		
		this.url = url;
		this.subLocalPath = subLocalPath;
		
		this.handler = handler;
		this.tag = tag;
		
		this.isNetworkAvailable = isNetworkAvailable;
	}
	
	
	public DownLoadThread(String url, String default_url, Handler handler, String tag, String subLocalPath, boolean isNetworkAvailable) {
		
		this.url = url;
		this.default_url = default_url;
		this.subLocalPath = subLocalPath;
		
		this.handler = handler;
		this.tag = tag;
		
		this.isNetworkAvailable = isNetworkAvailable;
	}

	
	@Override
	public void run() {
		super.run();
		
		// 下载
		try {
			
			// 文件名
			String file_postfix = url.substring(url.lastIndexOf("."));
			fileName = EncryptHelper.md5(url).concat(file_postfix);
			
			// 得到本地文件保存路径
			localPath = Helper.isNotEmpty(subLocalPath) ? ActivityHelper.getBaseCachePath().concat(subLocalPath) : ActivityHelper.getBaseCachePath();
//			LogUtil.i(TAG, "localPath: " + localPath);
			
			// 不存在时下载
			File file = new File(localPath + fileName);
//			LogUtil.i(TAG, "本地文件: " + localPath + fileName);
			if(!file.exists()) {
				if (isNetworkAvailable) {
//					LogUtil.i(TAG, "下载图片url: " + url);
					boolean isFileExist = HttpClientUtils.downloadImage(url, localPath, fileName);
					
					// 不存在时尝试默认图片
					if(!isFileExist && Helper.isNotEmpty(default_url)) {
						isUseDefault = true;
						HttpClientUtils.downloadImage(default_url, localPath, fileName);
//						LogUtil.i(TAG, "尝试default_url: " + default_url);
					}
				}
			}
			
			sendMessage();
			
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		
	}
	
	
	private void sendMessage() {
	
		// 发送更新消息
		Message msg = handler.obtainMessage();
		msg.what = MSG_KEY_DOWNIMAGE_SUCCESS;
		msg.obj = localPath + fileName;
		Bundle data = new Bundle();
		if(isUseDefault) {
			data.putString(PARAM_KEY_DOWNIMG_SERVERPATH, default_url);
		}
		else {
			data.putString(PARAM_KEY_DOWNIMG_SERVERPATH, url);
		}
		data.putString(PARAM_KEY_DOWNIMG_LOCALPATH, localPath);
		data.putString(PARAM_KEY_DOWNIMG_FILENAME, fileName);
		
		if(null != tag && !"".equals(tag)) {
			
			data.putString(PARAM_KEY_DOWNIMG_TAG, tag);
		}
		msg.setData(data);
		
		handler.sendMessage(msg);
	}
}
